<?php
/*!
 * @version		$Id: application.php 8841 2007-12-07 20:14:22Z $
 * @package		diduo: Makes the friend community in Wuhan 
 * @link        http://www.diduo.net
 * @author      moolao <moolao@diduo.net>
 * @abstract    application.php: system controller core class
 * @discussion  This file is all user app Control center, Easily do not revise it.
 * @copyright	Copyright (C) 2007 - 2008 diduo.net development team. All rights reserved.
 */

/* $Id: application.php, v 0.1.0 20:14:22 Exp $ */

// no direct access
defined( '_DEXEC' ) or die( 'Restricted access' );

// set the directory separator
defined( 'DS' ) || define( 'DS', DIRECTORY_SEPARATOR );

// import the configuration file
require ( DPATH_ROOT. DS. 'config.cfg.php' );
require ( DPATH_CONFIGURATION. DS. 'database.cfg.php' );

/**
 * System core control class
 * @package diduo
 */
class Application extends Config
{
    /** @access private */
	private  $_root;
	/**#@-*/
	
	/** @access protected */
    /**#@-*/
	
	/**
     * Database operation name handle 
     */
	public  $selectRs = null;

    public  $defaultAction;

    /**
     * Initialization system app 
     *
     * @param string $root
     */
	public function __construct( $root = '' )
	{
		parent::__construct();

		if ( !defined( ACTIONSTR ) ) { define( 'ACTIONSTR', 'play' ); }
		if ( !defined( EXEKEYSTR ) ) { define( 'EXEKEYSTR', 'key' ); }
		if ( !empty( $root ) ) { $this->setRoot( $root ); }
	}

    /**
     * Set script execute directory
     * 
     * @param string $root
     */
	public function setRoot( $root = '' )
    {
		if ( empty($root) )   { $root = dirname( $_SERVER['SCRIPT_FILENAME'] ); }
        if ( !is_dir($root) ) { return parent::catchErr( 'Path Error!' ); }
        if ( substr($root, -1) != DS ) { $root .=  DS; }

        $this->_root = $root;  
    }

    /**
     * Set execute default page
     *
     * @param string $name
     */
    public function setDefaultAction( $name )
    {
        $this->defaultAction = $name;
    }

    /**
     * Run system func 
     * 
     * Run this application must be called this function.
     */
    public function run()
    {   $C = $this->_getAction();
		$this->importClass( 'app', 'controller', '.controller.php' );
		$this->importClass( $this->_getAction() , 'controller', '.controller.php' );
		$_page = new $C( $this );
		$_page->execute();
		exit ();
	}

    /**
     * Get the request param
     */
	private function _getAction()
	{ 
		$_name = (isset($_POST[ACTIONSTR])) ? $_POST[ACTIONSTR] : $_GET[ACTIONSTR];
		if (empty($_name)) 
			$_name = $this->defaultAction; 
		return $_name;
	}

	/**
     * Smarty template configuration
     * 
     * process smarty templates engins cfg, this param also may 
     * be changed the system's template disposition  
     * 
     * @param array $options
     * @return Object
     */
	public function tpl( $options = array() ) 
	{ 
        $this->importClass('Smarty','common/lib/smarty', '.class.php');
        $tpl = new Smarty(); 

		/* set the smarty env cfg. */
        $tpl->template_dir  = array_key_exists(  'template_dir',     $options )     
							? $options['template_dir'] 
							: DPATH_ROOT.DS. 'templates'.DS. 'default';
		$tpl->compile_dir   = array_key_exists(  'compile_dir',      $options )        
							? $options['compile_dir']
							: DPATH_ROOT.DS. 'templates_c';
		$tpl->cache_dir     = array_key_exists(  'cache_dir',        $options )            
							? $options['cache_dir'] 
							: DPATH_ROOT. DS. 'cache';
		$tpl->left_delimiter = array_key_exists( 'left_delimiter',   $options )  
							? $options['left_delimiter']  
							: '<!--{';
		$tpl->right_delimiter = array_key_exists( 'right_delimiter', $options )
							? $options['right_delimiter'] 
							: '}-->'; 
        return $tpl;
	}

    /**
     * Adodb database configuration
     * 
     * This func process adodb cfg, this param also may 
     * be changed the system's template disposition, used   
     * adodb we can connect to multiple databases in a portable manner.
     * 
     * @param array $options
     * @return Object
     */
	public function databaseLink( $options = array() )
	{ 
		$_host      = array_key_exists('host', $options)	   ? $options['host']		: HOST_ADDR;
		$_user      = array_key_exists('user', $options)	   ? $options['user']		: HOST_USER;
		$_password  = array_key_exists('password', $options)   ? $options['password']	: HOST_PSW;
		$_database  = array_key_exists('database', $options)   ? $options['database']	: DB_NAME;

		$this->importClass('adodb', 'common/lib/adodb');
        $_conn =& ADONewConnection( ADAPTER );
	    $_conn->Connect( $_host, $_user, $_password, $_database);
	    return $this->selectRs = & $_conn;
	}

    /**
     * Load class file  
     * 
     * This func can load a class file form you library and you must be 
     * Assigns the class path and prefix
     * 
     * @param string $fileName
     * @param string $path
     * @param string $lastName
     * @return Object
     */
	public function importClass ($fileName, $path = 'common/lib', $lastName = '.inc.php')
	{
		if ( substr($path, 0, 1) != DS )  {  $path = DS . $path;  }
		if ( substr($path, 0, -1) != DS ) {  $path .= DS; }
		if ( empty( $fileName ) ) { 
			 return parent::catchErr( 'Error Class Name!' );
		}
		include( DPATH_ROOT . $path . $fileName . $lastName );
	}
} 
?>